<!--
 * @Descripttion: GOGOGO
 * @version: 1.0.0
 * @Author: XJM
 * @Date: 2022-08-15 10:30:30
 * @LastEditors: XJM
 * @LastEditTime: 2022-08-22 19:12:46
-->
<template>
    <div class="login_wrap">
        <div class="form_wrap">

            <el-form ref="formRef" :model="loginData" label-width="100px" class="demo-dynamic">
                <el-form-item prop="username" label="用户名" :rules="[
                    {
                        required: true,
                        message: '请输入用户名!',
                        trigger: 'blur',
                    },
                ]">
                    <el-input v-model="loginData.username" />
                </el-form-item>
                <el-form-item prop="password" label="密码" :rules="[
                    {
                        required: true,
                        message: '请输入密码!',
                        trigger: 'blur',
                    },
                ]">
                    <el-input type="password" v-model="loginData.password" />
                </el-form-item>
            </el-form>
            <el-button class="login_btn" type="primary" @click="handlelogin">登录</el-button>
        </div>
    </div>
</template>

<script>
import { reactive, toRefs } from 'vue';
import { useStore } from 'vuex';
import { useRouter } from 'vue-router';
import { loginApi } from '@/util/request';

export default {
    name: 'login',
    setup() {
        const store = useStore()
        const router = useRouter()
        // const count = store.state.number.count
        const data = reactive({
            loginData: {
                username: '',
                password: ''
            },
        })

        const handlelogin = () => {
            // 请求后台接口
            // 默认用户：admin  123456
            loginApi(data.loginData).then(res => {
                // console.log("登录的数据", res);
                if (res.data) {
                    store.commit('setUserInfo', res.data);
                    localStorage.setItem('loginData', JSON.stringify(res.data))
                    // 跳转到/index
                    router.push({
                        path: '/index'
                    })
                }
            })
        }


        // vuex更改语法
        // // console.log('修改前getters', store.getters["number/countStatus"]);
        // const handlelogin = () => {
        //     // store.commit('number/setCount', 100)
        //     store.dispatch('number/setCountPromise', 101).then(res => {
        //         alert('修改成功')
        //     }).catch(err => {
        //         alert(err)
        //     })
        //     // console.log(store.state.number.count);
        //     // console.log('修改后getters', store.getters["number/countStatus"]);
        // }
        return {
            ...toRefs(data),
            handlelogin
        }
    }
}
</script>

<style scoped>
.login_wrap {
    width: 100%;
    height: 100vh;
    background: rgb(54, 53, 100);
    position: relative;
}

.form_wrap {
    width: 400px;
    height: 180px;
    position: fixed;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    background: #fff;
    padding: 30px 50px;
    border-radius: 5px;
}

.login_btn {
    display: block;
    margin-left: 100px;
    width: 220px;
    border-radius: 15px;
}

.demo-dynamic {
    width: 320px;
}
</style>